// pages/category/category.js
import {getSystemSize} from "../../utils/system";
import {px2rpx} from "../../miniprogram_npm/lin-ui/utils/util";
import {Category} from "../../model/category";
import {SpuPaging} from "../../model/spu-paging";

Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.initCategoryData()
        this.setDynamicSegmentHeight()
    },
    async initCategoryData() {
        const category = new Category()
        this.data.category = category
        await category.getAll()
        let roots = category.getRoots()
        debugger
        let defaultRoot = category.getCurrentRoot()
        const currentSubs = category.getSubs(defaultRoot.id)
        this.setData({
            roots,
            currentSubs,
            currentBannerImg: defaultRoot.picUrl
        })
    },

    async setDynamicSegmentHeight() {
        const res = await getSystemSize()
        const windowHeightRpx = px2rpx(res.windowHeight)
        const h = windowHeightRpx - 60 - 20 - 2
        this.setData({
            segHeight: h
        })
    },

    onSegChange(event) {
        const rootId = event.detail.activeKey
        console.log(rootId)
        const currentSubs = this.data.category.getSubs(rootId)
        const currentRoot = this.data.category.getRoot(rootId)

        this.setData({
            currentSubs,
            currentBannerImg:currentRoot.picUrl
        })
    },

    onJumpToSpuList(event) {
        const cid = event.detail.cid;

        wx.navigateTo({
            url: `/pages/spu-list/spu-list?cid=${cid}`
        })
    },

    onGotoSearch(event) {
        wx.navigateTo({
            url: `/pages/search/search`
        })
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})